package com.xuecheng.manage_cms.web.controller;

import com.xuecheng.api.cms.CmsPageControllerApi;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.service.PageService;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gongfeiyue
 * @date 2020/5/18 11:09
 * @Description
 */
@RestController
@RequestMapping(value = "/cms/page")
@Slf4j
public class CmsPageController implements CmsPageControllerApi{

  @Autowired
  PageService pageService;

  /**
  * @Author: gongfeiyue
  * @Param:  page 页数  size 每页显示条数  queryPageRequest 查询条件参数
  * @Return: QueryResponseResult 存放响应列表list和总记录数total
  * @Date: 2020/7/7 14:54
  * @Description: 分页查询方法
  **/
  @Override
  @GetMapping(value = "/list/{page}/{size}")
  public QueryResponseResult findList(@PathVariable(name = "page") int page, @PathVariable(name = "size")int size, QueryPageRequest queryPageRequest) {
    QueryResponseResult queryResponseResult = pageService.findList(page, size, queryPageRequest);
    log.info("查询结构："+queryResponseResult.getQueryResult());
    return queryResponseResult;
  }

  /**
  * @Author: gongfeiyue
  * @Param:  CmsPage
  * @Return: CmsPageResult   继承ResponseResult 包含success code message 还有cmsPage
  * @Date: 2020/7/7 14:52
  * @Description: 页面新增方法
  **/
  @Override
  @PostMapping(value = "/add")
  public CmsPageResult add(@RequestBody CmsPage cmsPage) {
    CmsPageResult cmsPageResult = pageService.add(cmsPage);
    return cmsPageResult;
  }

  @Override
  @GetMapping(value = "/get/{pageId}")
  public CmsPage findCmsPageById(@PathVariable(name = "pageId") String pageId) {
    return pageService.findCmsPageById(pageId);
  }
  /**
  * @Author: gongfy
  * @Param:  CmsPage
  * @Return:  CmsPageResult 继承ResponseResult 包含success code message 还有cmsPage
  * @Date: 2020/7/8 14:57
  * @Description: 页面修改方法  1.先根据id查询页面信息  2.保存页面信息
  **/
  @Override
  @PostMapping(value = "/modify/{pageId}")
  public CmsPageResult modifyCmspage(@RequestBody CmsPage cmsPage,@PathVariable(name = "pageId") String pageId) {
    // todo 传入的pageid 和 cmsPage.getPageId() 有啥区别？
    log.info("pageId和cmsPage.getPageId()是否相等："+pageId.equals(cmsPage.getPageId()));
    return pageService.modify(pageId,cmsPage);
  }

  @Override
  @DeleteMapping(value = "/delete/{pageId}")
  public ResponseResult deleteById(@PathVariable(name = "pageId") String pageId) {
    return pageService.deleteById(pageId);
  }

  //页面发布 将html文件保存到服务器的指定目录
  @Override
  @PostMapping(value = "/pagepublish/{pageId}")
  public ResponseResult pagePublish(@PathVariable(name = "pageId") String pageId) {
    return pageService.pagePublish(pageId);
  }

  //页面保存
  @Override
  @PostMapping(value = "/save")
  public CmsPageResult savePage(@RequestBody CmsPage cmsPage) {
    return pageService.savePage(cmsPage);
  }

}
